package com.hw.dagl.controller;

import static com.hw.utils.DESCrypt.encrypt;
import static com.hw.utils.DESCrypt.toHexString;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hw.dagl.model.Sys_Role;
import com.hw.dagl.model.Sys_User_Log;
import com.hw.dagl.model.Vehicle;
import com.hw.dagl.service.SysUserLogService;
import com.hw.dao.utils.ControllerUtil;
import com.hw.dao.utils.Page;
import com.hw.dao.utils.PageUtil;

@Controller
public class SysUserLogController extends BaseController {

	@Resource
	private SysUserLogService sysUserLogServiceImpl;
	
	@RequestMapping(method=RequestMethod.GET)
	public String logHome(HttpServletRequest request, HttpServletResponse response){
		return "syslog/sysuserlog";
	}
	
	@RequestMapping(method=RequestMethod.POST)
	public void findSysuserlog(HttpServletRequest request,HttpServletResponse response,Sys_User_Log info,Page<Sys_User_Log> data){
		Page<Sys_User_Log> list = sysUserLogServiceImpl.findSysuserlog(data, info);
		ControllerUtil.renderJson(response, list, "encoding:UTF-8", "no-cache:false");
	}
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView findYxsqxzByxhFirstIn(HttpServletRequest request,HttpServletResponse response,
			Vehicle info,String startTime,String endTime){
	    ModelAndView mv = new ModelAndView("syslog/jdcViewFlag");
		info =sysUserLogServiceImpl.findYxxxglByxh(info,startTime,endTime);
		Document doc=null;
		try {
			doc = DocumentHelper.parseText(info.getTpurl()==null?"":info.getTpurl());
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}
		List<Vehicle> listTpurl = new ArrayList<Vehicle>(); //存放图片列表对象
		Map<Integer,Integer> map = new HashMap<Integer,Integer>();
		 int registerNum=8;             		 //注册--A
		    int transformNum = 5 ,changeNum =5;  //转移，变更和补换领  --B, D、K、L
		    int mortgageNum=7;             		 //抵押和解除抵押 --E
		    int logoutNum=4;					//注销--G
		    int changeIntoNum=5;				//转入--I
		    if(info.getYwlx().equals("A")){
				for(int i=0;i<registerNum;i++){ //初始化不同业务下各档案类型
					map.put(i, 0);
				}
		    }else if(info.getYwlx().equals("B")){
		    	for(int i=0;i<changeNum;i++){ //初始化不同业务下各档案类型
					map.put(i, 0);
				}
		    }else if(info.getYwlx().equals("D") || info.getYwlx().equals("K") || info.getYwlx().equals("L")){
		    	for(int i=0;i<transformNum;i++){ //初始化不同业务下各档案类型
					map.put(i, 0);
				}
		    }else if(info.getYwlx().equals("E")){
		    	for(int i=0;i<mortgageNum;i++){ //初始化不同业务下各档案类型
					map.put(i, 0);
				}
		    }else if(info.getYwlx().equals("G")){
		    	for(int i=0;i<logoutNum;i++){ //初始化不同业务下各档案类型
					map.put(i, 0);
				}
		    }else if(info.getYwlx().equals("I")){
		    	for(int i=0;i<changeIntoNum;i++){ //初始化不同业务下各档案类型
					map.put(i, 0);
				}
		    }

			map.put(100, 0);
			
		if(null!=doc){
			Element root = doc.getRootElement();
			List nodes = root.elements();
			for (Iterator<?> it = nodes.iterator(); it.hasNext();) {
				Vehicle obj =new Vehicle();
				Element e = (Element) it.next();
				String key = e.attributeValue("id");
				obj.setId(key);
				try {
					obj.setSingleIpaddress(toHexString(encrypt(e.getStringValue(), "ahhwxxjs")).toUpperCase());
				} catch (Exception e1) {
					e1.printStackTrace();
				}
								
				listTpurl.add(obj);			
			}
			for( Entry<Integer, Integer> mapTemp : map.entrySet()){
				for(Vehicle list : listTpurl){
					if(mapTemp.getKey().toString().equals(list.getId())){
						map.put(mapTemp.getKey(), mapTemp.getValue().intValue()+1);
					}
					if(mapTemp.getKey().toString().equals("100")){
						map.put(mapTemp.getKey(), mapTemp.getValue().intValue()+1);
					}
					
				}					
			}
		}
		mv.addObject("tpNums", map);
		mv.addObject("listTpurl",listTpurl);
		mv.addObject("info",info);
		return mv;
	}
}
